<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/7
 * Time: 14:12
 */

namespace app\api\controller;


class ProductUpdate extends Base
{

    /**
     * @name 客户端返回更新包路径
     * @author wx
     * @date 2019/11/7 14:40
     */
    public function getPackagePath()
    {
        //接收参数
        $data = base64_decode(file_get_contents("php://input"));
        //$data = '{"type":"client","os":"linux","version":"3.0.0", "hostkey":"05a0191ccfba05007761487eef1b1596"}';
        $data = json_decode($data,true);


        //检查文件大小
        $fileLog = '../runtime/log/ServerUpdate.log';
        if (file_exists($fileLog) && filesize($fileLog) >= 5 * 1024 * 1024 * 1024) {
            unlink($fileLog);
        }

        $params = $_SERVER['REMOTE_ADDR'].'返回更新包路径:'.date('Y-m-d H:i:s',time()).PHP_EOL.json_encode($data,JSON_UNESCAPED_UNICODE).PHP_EOL;
        file_put_contents($fileLog,$params,FILE_APPEND);

        $server = new \app\api\service\ProductUpdate();
        $list = $server->getPackagePath($data);

        $res = 'PHP返回数据'.date('Y-m-d H:i:s',time()).PHP_EOL.json_encode($list,JSON_UNESCAPED_UNICODE).PHP_EOL;
        file_put_contents($fileLog,$res,FILE_APPEND);

        $this->jsonOutPut($list);
    }

    //下载结果回传
    public function downloadResult()
    {
        //接收参数
        $data = base64_decode(file_get_contents("php://input"));
        $data = json_decode($data,true);

        $server = new \app\api\service\ProductUpdate();
        $list = $server->downloadResult($data);

        $this->jsonOutPut($list);
    }

    //更新包执行成功
    public function updateResult()
    {
        //接收参数

        $data = base64_decode(file_get_contents("php://input"));

        if(!empty($data)){

            $data = json_decode($data,true);
        }else{//linux

            $data = input('get.');
        }

        $server = new \app\api\service\ProductUpdate();
        $list = $server->updateResult($data);

        $this->jsonOutPut($list);
    }

    //服务端,客户端
    public function timeOut()
    {
        $server = new \app\api\service\ProductUpdate();
        $count = $server->timeOut();

        echo $count;
    }

}